using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;

namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CViClip : Class
    {
        #region Declares
        int _videoid = -1;
        int _catalogid = -1;
        string _videotitle = "";
        string _description = "";
        int _userviewer = -1;
        DateTime _timeupdate = DateTime.Now;
        string _linkvideo = "";
        string _logovideo = "hcgd.png";

        public string Logovideo
        {
            get { return _logovideo; }
            set { _logovideo = value; }
        }
        #endregion

        #region Constructors
        public CViClip()
        {
        }
        public CViClip(CLogin Login)
        {
            base.Login = Login;
        }
        public CViClip(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBViClip db = new AccDBViClip())
            {
                this.Populate((CViClip)db.GetClass(Login, ObjId));
            }
        }
        public CViClip(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CViClip(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBViClip db = new AccDBViClip())
            {
                this.Populate((CViClip)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("videoid") >= 0)
                    {
                        this._videoid = (dr["videoid"] != DBNull.Value ? Convert.ToInt32(dr["videoid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD videoid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("catalogid") >= 0)
                    {
                        this._catalogid = (dr["catalogid"] != DBNull.Value ? Convert.ToInt32(dr["catalogid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD catalogid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("videotitle") >= 0)
                    {
                        this._videotitle = (dr["videotitle"] != DBNull.Value ? Convert.ToString(dr["videotitle"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD videotitle [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("description") >= 0)
                    {
                        this._description = (dr["description"] != DBNull.Value ? Convert.ToString(dr["description"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD description [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("userviewer") >= 0)
                    {
                        this._userviewer = (dr["userviewer"] != DBNull.Value ? Convert.ToInt32(dr["userviewer"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD userviewer [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("timeupdate") >= 0)
                    {
                        this._timeupdate = (dr["timeupdate"] != DBNull.Value ? Convert.ToDateTime(dr["timeupdate"]) : DateTime.Now);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD timeupdate [datetime] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("linkvideo") >= 0)
                    {
                        this._linkvideo = (dr["linkvideo"] != DBNull.Value ? Convert.ToString(dr["linkvideo"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD linkvideo [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("logovideo") >= 0)
                    {
                        this._logovideo = (dr["logovideo"] != DBNull.Value ? Convert.ToString(dr["logovideo"]) : "hcgd.png");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblViClip ADD linkvideo [nvarchar] (50) NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CViClip Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._videoid = Obj.videoid;
                    this._catalogid = Obj.catalogid;
                    this._videotitle = Obj.videotitle;
                    this._description = Obj.description;
                    this._userviewer = Obj.userviewer;
                    this._timeupdate = Obj.timeupdate;
                    this._linkvideo = Obj.linkvideo;
                    this._logovideo = Obj.Logovideo;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBViClip db = new AccDBViClip())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBViClip db = new AccDBViClip())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBViClip db = new AccDBViClip())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBViClip db = new AccDBViClip())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override Form PrintPreview()
        {
            throw (new NotSupportedException("Phuong thuc nay hien chua duoc cai dat!"));
        }
        public override bool Print()
        {
            throw (new NotSupportedException("Phuong thuc nay hien chua duoc cai dat!"));
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CViClip obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CViClip ViClip);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CViClip ViClip);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CViClip ViClip);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CViClip ViClip);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CViClip obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int videoid
        {
            get { return this._videoid; }
            set
            {
                if (this._videoid != value)
                {
                    this._videoid = value;
                }
            }
        }
        public int catalogid
        {
            get { return this._catalogid; }
            set
            {
                if (this._catalogid != value)
                {
                    this._catalogid = value;
                }
            }
        }
        public string videotitle
        {
            get { return this._videotitle; }
            set
            {
                if (this._videotitle != value)
                {
                    this._videotitle = value;
                }
            }
        }
        public string description
        {
            get { return this._description; }
            set
            {
                if (this._description != value)
                {
                    this._description = value;
                }
            }
        }
        public int userviewer
        {
            get { return this._userviewer; }
            set
            {
                if (this._userviewer != value)
                {
                    this._userviewer = value;
                }
            }
        }
        public DateTime timeupdate
        {
            get { return this._timeupdate; }
            set
            {
                if (this._timeupdate != value)
                {
                    this._timeupdate = value;
                }
            }
        }
        public string linkvideo
        {
            get { return this._linkvideo; }
            set
            {
                if (this._linkvideo != value)
                {
                    this._linkvideo = value;
                }
            }
        }
        #endregion
    }
    #region Class AccDB
    public class AccDBViClip : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBViClip()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT videoid, ViClipName";
                sql += " FROM  tblViClip";
                sql += " WHERE (videoid >= 0)";
                sql += " ORDER BY ViClipName";
                DataTable table = p.OpenTable(sql, "tblViClip");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["ViClipName"] = extName;
                        row["videoid"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "ViClipName";
                    cmb.ValueMember = "videoid";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblViClip WHERE (videoid = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblViClip");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CViClip(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CViClip ViClip = (CViClip)obj;
            string sql = "SELECT * FROM tblViClip";
            sql += " WHERE videoid =" + ViClip.videoid + "";
            DataTable dt = p.OpenTable(sql, "tblViClip");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && ViClip.videoid != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    ViClip.videoid = Convert.ToInt32(p.InitIDKey("videoid", "tblViClip", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["videoid"] = ViClip.videoid;
                row["catalogid"] = ViClip.catalogid;
                row["videotitle"] = ViClip.videotitle;
                row["description"] = ViClip.description;
                row["userviewer"] = ViClip.userviewer;
                row["timeupdate"] = ViClip.timeupdate;
                row["linkvideo"] = ViClip.linkvideo;
                row["videostatus"] = 1;
                row["logovideo"] = ViClip.Logovideo;
                if (!p.UpdateTable("tblViClip", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CViClip ViClip = (CViClip)obj;
            string sql = "DELETE FROM tblViClip";
            sql += " WHERE videoid =" + ViClip.videoid + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblViClip ORDER BY ViClipName";
            DataTable table = p.OpenTable(sql, "tblViClip");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CViClip o = new CViClip(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public CCollection Search(CLogin Login, string value)
        {
            CCollection Collection = new CCollection();
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT * FROM tblViClip ORDER BY timeupdate DESC";
                DataTable table = p.OpenTable(sql, "tblViClip");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CViClip o = new CViClip(Login, p, row);
                        Collection.Add(o);
                    }
                }
                p.Close();
            }
            return Collection;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <returns></returns>
        public CCollection SearchTopViewer(CLogin Login)
        {
            CCollection Collection = new CCollection();
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT TOP 50 * FROM tblViClip ORDER BY userviewer DESC";
                DataTable table = p.OpenTable(sql, "tblViClip");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CViClip o = new CViClip(Login, p, row);
                        Collection.Add(o);
                    }
                }
                p.Close();
            }
            return Collection;
        }
    }
    #endregion
}
